// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.IO;
using svs.Mobile.BeyondMedia.FeedCore;

namespace svs.Mobile.BeyondMedia.FeedCore.RSS.Entities
{
	/// <summary>
	/// Identifies the enclosure of a RSS feed item.
	/// </summary>
	public class RssEnclosure
	{
		private string m_url;
		private long? m_length;
		private string m_type;
		private readonly string m_DownloadPath;
		private readonly bool m_ForceUniqueEnclosureNames;
		private RssFeedItem m_Parent;

		internal RssEnclosure(string url, long startSize)
		{
			m_url = url;
			m_length = startSize;
		}

		public RssEnclosure(string i_DownloadPath, bool i_forceUniqueNames)
		{
			m_url = String.Empty;
			m_type = String.Empty;
			m_DownloadPath = i_DownloadPath??String.Empty;
			m_ForceUniqueEnclosureNames = i_forceUniqueNames;
		}

		public override bool Equals(object obj)
		{
			if (this == obj) return true;
			RssEnclosure rssEnclosure = obj as RssEnclosure;
			if (rssEnclosure == null) return false;
			return Equals(m_url, rssEnclosure.m_url) && Equals(m_DownloadPath, rssEnclosure.m_DownloadPath);
		}

		public override int GetHashCode()
		{
			return (m_url != null ? m_url.GetHashCode() : 0) + 29*(m_DownloadPath != null ? m_DownloadPath.GetHashCode() : 0);
		}

		#region Properties

		/// <summary>
		/// Url to get to the eclosure
		/// </summary>
		public string Url
		{
			get { return m_url; }
			set { m_url = value; }
		}

		/// <summary>
		/// Size in bytes
		/// </summary>
		public long? Length
		{
			get { return m_length; }
			set { m_length = value; }
		}

		/// <summary>
		/// Type of the enclosure
		/// </summary>
		public string Type
		{
			get { return m_type; }
			set { m_type = value; }
		}

		/// <summary>
		/// True if the enclosure is downloaded to the device
		/// </summary>
		public bool Downloaded
		{
			get
			{
				Track t = FeedRepository.GetTrackByPath(LocalFileName);
				if (t != null)
					return t.IsFullyDownloaded;

				return false;
			}
		}

		/// <summary>
		/// True if the enclosure has been downloaded to the device before
		/// </summary>
		public bool EnclosureInHistory
		{
			get { return EnclosureDownloadHistory.HasEntryFor(LocalFileName); }
		}

		/// <summary>
		/// THe local file name if the enclosure is downloaded
		/// </summary>
		public string LocalFileName
		{
			get
			{
				return Path.Combine(m_DownloadPath, FileName);
			}
		}

		/// <summary>
		/// Enclosure File Name
		/// </summary>
		public string FileName
		{
			get
			{
				Feed feed = FeedRepository.GetFeedById(m_Parent.OriginatingFeedID);
				string prefix = feed != null ? feed.Name : string.Empty;
				return CoreHelper.CreateFileNameFromUrl(Url, m_type, m_ForceUniqueEnclosureNames, prefix);
			}
		}

		/// <summary>
		/// Enclosure File Name as string (3 Mb)
		/// </summary>
		public string EnclosureSizeAsString
		{
			get { return CoreHelper.GetFileLengthAsString(Length); }
		}

		/// <summary>
		/// THe date this enclosure was published (same as feed item's pub date). THe Date is In UTC
		/// </summary>
		public DateTime? PubDate
		{
			get
			{
				if (m_Parent != null)
					return m_Parent.PubDate;

				return null;
			}
		}

		/// <summary>
		/// THe owner of the enclosure
		/// </summary>
		public RssFeedItem Parent
		{
			get { return m_Parent; }
			set { m_Parent = value; }
		}

		#endregion
	}
}